import Vue from 'vue'

import Vuex from 'vuex'

import request from './api/request'

Vue.use(Vuex)
// 音乐播放相关常量
const MusicAudio = uni.createInnerAudioContext();

const store = new Vuex.Store({
    state: {
		//公共的变量，这里的变量不能随便修改，只能通过触发mutations的方法才能改变
		user: {
		  account: '',
		  token: '',
		},
		MusicPlayState: false,
		// 当前音乐播放数据
		currentMusic:{
				"cover":"https://yunsheng.buzz/qyapp/static/img/defaultHeadImg.svg",
				"name":"暂无歌曲",
				"url":"",
				"author":''
		},
		// 最近播放
		recentlyMusic:[],
		// 循环播放控制器
		musicWhile:null
	},
    mutations: {
		//相当于同步的操作
		loginUser(state, { account, token }) {
		  state.user.account = account;
		  state.user.token = token;
		  request.setToken(token);
		},
		logOut(state){
			state.user.account = "";
			state.user.token = "";
			request.setToken("");
		},
		playMusic(state, {cover,name,author,url}) {
			if (url == null || url == undefined) {
				uni.showToast({
					title:'🥀'+"选择音乐后播放哦",
					icon:'error',
					duration:2000
				})
				return;
			}
			// 不同歌曲切换播放
			if (MusicAudio.src == undefined || MusicAudio.src != url) {
				// 取消上一个歌曲的定时函数
				clearInterval(state.musicWhile);
				state.currentMusic.author = author;
				state.currentMusic.name = name;
				state.currentMusic.cover = request.baseServer + cover;
				state.currentMusic.url = request.baseServer + url;
				MusicAudio.src = state.currentMusic.url;
				// MusicAudio.autoplay = true;
				// state.MusicPlayState = true;
				// 添加到最近播放
				state.recentlyMusic.push({
					name:name,
					author:author,
					url:url,
					cover:cover
				});
			}
			// 同一首歌进行状态判断
			if (MusicAudio.paused) {
				MusicAudio.play()
				state.MusicPlayState = true;
			} else {
				MusicAudio.pause() //暂停
				state.MusicPlayState = false;
			}
			// 设置定时函数 重复播放
			state.musicWhile = setInterval (state => {
				if (state.MusicPlayState == true){
					if (MusicAudio.paused == false){
						MusicAudio.play()
					}
				}
			}, 100);
		},
		// 删除最近播放
		removeRecentlyMusic(state,{index}){
			state.recentlyMusic.splice(index, 1);
		}
	},
    getters: {
		// 检测是否登录
		isLogin: state => {
			if ( state.user.account != null 
				&& state.user.account != "" 
				&& state.user.token != null 
				&& state.user.token != "") {
				return true;
			}
			// 未登录跳转用户界面
			uni.showToast({
					title: '🥀未登录',
					icon: 'none',
					duration: 500,
					position:"bottom"
				})
			return false;
		},
		// 获取默认用户
		getUser: state => {
			return state.user;
		},
		// 获取默认用户
		getDefaultUser: state => {
			return {
				name: "游客",
				img:"/static/img/defaultHeadImg.svg",
			};
		},
		// 获取当前播放音乐
		getCurrentMusic: state => {
			return state.currentMusic;
		},
		// 获取当前播放状态
		getAudioState: state => {
			return state.MusicPlayState;
		},
		// 获取最近播放
		getRecentlyMusic: state => {
			return state.recentlyMusic;
		}
	}
})
export default store

/**
 * 
 * 修改
		this.$store.commit(
			'removeRecentlyMusic',
			{
				name:"大板鲫",
				auth:"dfjlasj"
			}
		)
	查询
		console.log(this.$store.getters.isLogin);
		console.log(this.$store.getters.getUser.getRecentlyMusic);
 */